#pragma once 
#include<iostream>
#include<memory>
#include<sys/epoll.h>
#include "Socket.hpp"
#include "Epoller.hpp"
#include "Log.hpp"
#include "nocopy.hpp"

uint32_t EVENT_IN = (EPOLLIN);
uint32_t EVENT_OUT = (EPOLLOUT);

class EpollServer : public nocopy
{
    static const int num = 64;
public:
    EpollServer(uint16_t port)
        :_port(port),
        _listensocket_ptr(new Sock()),
        _epoller_ptr(new Epoller())
        {}

    void Init()
    {
        _listensocket_ptr->Socket();
        _listensocket_ptr->Bind(_port);
        _listensocket_ptr->Listen();

        lg(Info, "create lisetn socket success: %d\n", _listensocket_ptr->Fd());
    }

    void Accepter()
    {
        std::string clientip;
        uint16_t clientport;
        int sock = _listensocket_ptr->Accept(&clientip, &clientport);
        if(sock > 0)
        {
            _epoller_ptr->EpollerUpdate(EPOLL_CTL_ADD, sock, EVENT_IN);
            lg(Info, "get a new link, client info@%s:%d", clientip.c_str(), clientport);
        }
    }

    void Recver(int fd)
    {
        char buffer[1024];
        ssize_t n = read(fd, buffer, sizeof(buffer) - 1);
        if(n > 0)
        {
            buffer[n] = 0;
            std::cout << "get a message: " << buffer << std::endl;
            std::string echo_str = "server echo $ ";
            write(fd, echo_str.c_str(), echo_str.size());
        }
        else if(n = 0)
        {
            lg(Info, "client quit, me too, close fd is %d", fd);
            _epoller_ptr->EpollerUpdate(EPOLL_CTL_DEL, fd, 0);
            close(fd);
        }
        else
        {
            lg(Warning, "recv error: fd is : %d", fd);
            _epoller_ptr->EpollerUpdate(EPOLL_CTL_DEL, fd, 0);
            close(fd);
        }
    }

    void Dispatcher(struct epoll_event revs[], int num)
    {
        for(int i = 0; i < num; i++)
        {
            uint32_t events = revs[i].events;
            int fd = revs[i].data.fd;
            if(events & EVENT_IN)
            {
                if(fd == _listensocket_ptr->Fd())
                {
                    Accepter();
                }
                else 
                {
                    Recver(fd);
                }
            }
            else if(events & EVENT_OUT)
            {

            }
            else 
            {

            }
        }
    }

    void Start()
    {
        _epoller_ptr->EpollerUpdate(EPOLL_CTL_ADD, _listensocket_ptr->Fd(), EVENT_IN);
        struct epoll_event revs[num];
        for(;;)
        {
            int n = _epoller_ptr->EpollerWait(revs, num);
            if(n > 0)
            {
                lg(Debug, "event happened, fd is : %d", revs[0].data.fd);
                Dispatcher(revs, n);
            }
            else if(n == 0)
            {
                lg(Info, "time out ...");
            }
            else
            {
                lg(Error, "epll wait errro");
            }
        }
    }

    ~EpollServer()
    {
        _listensocket_ptr->Close();
    }
private:
    std::shared_ptr<Sock> _listensocket_ptr;
    std::shared_ptr<Epoller> _epoller_ptr;
    uint16_t _port;
};